package com.atgugui.gulimall.search.thread;

import java.util.concurrent.*;

public class ThreadTest {
    public static ExecutorService pool = Executors.newFixedThreadPool(10);
    public static void main(String[] args) throws ExecutionException, InterruptedException {
//        ThreadPoolExecutor pool = new ThreadPoolExecutor();
//        Executors.newFixedThreadPool()

//        FutureTask<Integer> t = new FutureTask<>(new Callbale01());
//        new Thread(t).start();
//        System.out.println(t.get());


//        CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {
//            int a = 10;
//            int r = a / 0;
//            return r;
//        }, pool).whenCompleteAsync((res, exception) -> {
//            System.out.println("result>"+res+" ,exception>"+exception);
//        }).exceptionally(throwable -> {
//            System.out.println(throwable);
//            return 10;
//        });
//        System.out.println("future>"+future.get());

//        CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {
//            int a = 12/0;
//            return a;
//        }, pool).handleAsync((res,exc)->{
//            if(res != null){
//                return 10;
//            }
//            if(exc != null){
//                return 0;
//            }
//            return 1;
//        });
//        System.out.println("handle>"+future.get());
////        R apply(T t, U u);

        CompletableFuture<Integer> f = CompletableFuture.supplyAsync(() -> {

            return 1;
        }, pool).thenApplyAsync(res -> {
            System.out.println("apply>"+res);
            return 2;
        },pool);
        System.out.println(f.get());
    }

    static class Callbale01 implements Callable<Integer> {

        @Override
        public Integer call() throws Exception {
            int a = 10;
            int r = a/2;
            return r;
        }
    }
}
